package fun.yao.common.uitls;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 时间相关
 */
public class DateUtil {

    public final static SimpleDateFormat sdf_yyyyMMdd = new SimpleDateFormat("yyyyMMdd");
    public final static SimpleDateFormat sdf_yyyy_MM_dd = new SimpleDateFormat("yyyy-MM-dd");
    public final static SimpleDateFormat sdf_hhmmss = new SimpleDateFormat("hhmmss");
    public final static SimpleDateFormat sdf_yyyy_MM_dd_hh_mm_ss_SS = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss SS");

    public final static Calendar calendar = Calendar.getInstance();

    /**
     * 根据与当前日期差和时间格式获取时间
     *
     * @param day 0-当天
     * @param sdf 时间格式
     * @return
     */
    public static String getCalcDate(long day, SimpleDateFormat sdf) {
        Date now = new Date();
        if (day != 0) now.setTime(now.getTime() + day * 24 * 3600 * 1000L);
        return sdf.format(now);
    }

    public static String getCalcDate(long day,String dateStr,SimpleDateFormat sdf)throws ParseException{
        long dateMillis = getCalcDateMillis(dateStr,sdf);
        Date date = new Date(dateMillis);
        if (day != 0) date.setTime(dateMillis + day * 24 * 3600 * 1000L);
        return sdf.format(date);
    }

    /**
     * 获取指定时间的时间戳
     *
     * @param dateStr 2019-04-25 11:02:01
     * @param sdf  时间格式
     * @return
     */
    public static long getCalcDateMillis(String dateStr, SimpleDateFormat sdf) throws ParseException {
        calendar.setTime(sdf.parse(dateStr));
        return calendar.getTimeInMillis();
    }

    /**
     * 获取时间差时间戳
     * @param day 0-当天
     * @return
     */
    public static long getCalcDateMillis(long day) throws ParseException{
        calendar.setTime(sdf_yyyy_MM_dd.parse(getCalcDate(day,sdf_yyyy_MM_dd)));
        return calendar.getTimeInMillis();
    }

}
